package cn.hy.dataSync.config;

import lombok.Data;

import static com.google.common.base.Preconditions.checkNotNull;


/**
 * 切分的任务配置对象
 */
@Data
public class TaskConfig {

    /**
     * 所属 job 配置
     */
    private final JobConfig jobConfig;

    /**
     * 系统配置
     */
    private final CoreConfig coreConfig;

    /**
     * 当前任务 id
     */
    private final int taskId;

    /**
     * 数据读取 sql
     */
    private String readerSql;

    /**
     * 数据写入 sql
     */
    private String writerSql;


    public TaskConfig(final JobConfig jobConfig,
                      final String readerSql,
                      final String writerSql) {
        this.jobConfig = jobConfig;
        this.coreConfig = jobConfig.getCoreConfig();
        this.taskId = jobConfig.nextTaskId();
        int capacity = jobConfig.getCoreConfig()
                .getInteger(CoreConstant.FLOW_CONTROL_INTERVAL, 2048);
        if (capacity <= 0) {
            throw new IllegalArgumentException(String.format("通道容量[%d]必须大于0.", capacity));
        }
        this.readerSql = checkNotNull(readerSql, "readerSql 不能为空");
        this.writerSql = checkNotNull(writerSql, "readerSql 不能为空");
    }

    @Override
    public TaskConfig clone() throws CloneNotSupportedException {
        TaskConfig clone = (TaskConfig) super.clone();
        clone.setReaderSql(this.readerSql);
        clone.setWriterSql(this.writerSql);
        return clone;
    }
}
